<?php

namespace services\models;
use Yii;

class Cart extends \common\models\Cart
{  
    private  $time;
    private  $maxNum = 0;
    
    public function __construct() {
       $this->time = time();
       $this->maxNum = Yii::$app->params['maxNumber'];
    }
    
    
    public static  function getCartInfoById($id) {
       return  self::findOne($id);
    }
    
    
    /**
     * 判断加入购物车验证条件
     * @return boolean[]|string[]
     */
    public function validateLimit() 
    {   
        $msg = ['flag'=>false,'msg'=>'','data'=>['number'=>0]];        
        //查询库存信息
        $stockInfo = ProductStock::findByProductId($this->product_id);
        //获取商品信息
        $goodsInfo = Goods::findOne(['goods_id'=>$this->goods_id]);
        //获取规格信息
        $productInfo = Product::findByProductId($this->product_id,false);
        
        //判断商品是否下线
        if(empty($goodsInfo) || $goodsInfo->status == Goods::$_STATUS_OFF) {
            $msg['msg'] = '当前商品已经下线';
            return $msg;
        }
        //再判断产品库存
        if(empty($stockInfo) || $stockInfo['stock'] < 1 || empty($productInfo)) {
            $msg['msg'] = '库存不足';
            return $msg;
        }
        
        //查询当前产品规格是否已经下线
        if(empty($productInfo) || $productInfo->sale_status == Product::$_SALE_STATUS_OFF_SHELF) {
            $msg['msg'] = '当前商品规格已经下架';
            return $msg;
        }
        //判断当前数量不能小于1
        if($this->number < 1) {
            $msg['msg'] = '商品至少选购一件';
            $msg['data']['number'] = 1;
            return $msg;
        }
        //试用商品需要判断是否已经申领
        if($productInfo->type == Product::$_TYPE_TRY ) {
            //判断当前用户是否已经申领过该产品
            $flag = Goods::tryGoodsConfirm($this->goods_id, $this->bestry_uid);
            if($flag) {
                $msg['msg'] = '您已经申领该试用商品';
                return $msg;
            }
            
            //根据goodsid取出所有当前产品下面的试用全部规格
            $product_Arr = Product::find()
            ->select(['product_id'])
            ->where(['goods_id'=>$this->goods_id,'type'=>Product::$_TYPE_TRY])
            ->column();
            
            //试用商品的全部规格只能选其一个
            $cart_product_Arr = Cart::find()
            ->select(['product_id'])
            ->where(['bestry_uid'=>$this->bestry_uid,'goods_id'=>$this->goods_id])
            ->column();
            
            $array_intersect = array_intersect($product_Arr,$cart_product_Arr);
            
            if(!empty($array_intersect)) {
                $msg['msg'] = '您已申请过该商品';
                $msg['data']['number'] = 1;
                return $msg;
            }
            
            
            //免费试用产品只能数量一件
            if($this->number > 1) {
                $msg['msg'] = '试用商品限购一件';
                $msg['data']['number'] = 1;
                return $msg;
            }
            
            //获取member_no
            $member_no = MemberInfoBestry::getMemberIdByBestryUid($this->bestry_uid);
            
            //试用产品限购注册时间限制
            $register_limit = $productInfo->checkRegisterLimt($member_no);
            if(!$register_limit) {
                $msg['msg'] = $productInfo->getFirstError($this->goods_id);
                return $msg;
            }
            
        }
        
        //付费产品限购199件
        if($this->number > $this->maxNum && $productInfo['type'] == Product::$_TYPE_SALE) {
            $msg['msg'] = '库存不足';
            $msg['data']['number'] = $this->maxNum;
            return $msg;
            
        }
        //购买的商品数量大于库存最大量
        if($this->number > $stockInfo['stock'] ) {
            $msg['msg'] = '库存不足';
            $msg['data']['number'] = $stockInfo['stock'];
            return $msg;
        }
        
        $msg['flag'] = true;
        $msg['msg'] = 'success';
        return $msg;
        
    }
    
    public function getProduct()
    {
        return $this->hasOne(Product::className(), ['product_id'=>'product_id']);
    }

    public function getProductDetail()
    {
        return $this->hasOne(ProductDetail::className(), ['product_id'=>'product_id']);
    }

    public function getProductStock()
    {
        return $this->hasOne(ProductStock::className(), ['product_id'=>'product_id']);
    }

    public function getGoods()
    {
        return $this->hasOne(Goods::className(), ['goods_id'=>'goods_id']);
    }

    public function getGoodsDetail()
    {
        return $this->hasOne(GoodsDetail::className(), ['goods_id'=>'goods_id']);
    }
    
    /**
     * 获取当前用户下面购物车数量，不限制过期商品
     * @param int $bestry_uid
     */
    public  static  function getUserCartNum($bestry_uid){
      $val =  self::find()->select(['number'])->where(['bestry_uid'=>$bestry_uid])->column();
      if(!$val) {
          return 0;
      }
      return array_sum($val);
    }

}

?>